package com.zhangtai.modules.controller.admin;

import java.io.OutputStream;
import java.text.ParseException;
import java.util.*;

import com.zhangtai.modules.dto.ExportQwCheckMonthTableDto;
import com.zhangtai.modules.entity.qw.QwCheckinsEntity;
import com.zhangtai.modules.service.qw.QwCheckinsService;
import com.zhangtai.utils.Excel.ExcelUtil;
import com.zhangtai.utils.PageUtils;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;


/**
 * @author wanwgei
 * @email i@weiwang.com
 * @date 2021-05-24 21:04:26
 */
@RestController
@RequestMapping("qwcheckins")
@Api(tags = "OA打卡信息")
public class QwCheckinsController {
    @Autowired
    private QwCheckinsService qwCheckinsService;

    /**
     * 获取员工的打卡数据
     *
     * @return
     */
    @GetMapping("/QwCheckinData")
    @ApiOperation("获取员工考勤数据")
    public R getQwCheckinData(@RequestParam("starttimes") int starttimes, @RequestParam("endtimes") int endtime, @RequestParam(value = "userids", required = false) String[] userids
    ) {
        List<QwCheckinsEntity> list = null;
        try {
            list = qwCheckinsService.getQwCheckinData(starttimes, endtime, userids);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return R.ok(list);
    }


    @ApiOperation("获取员工打卡报表")
    @Cacheable(value = "checkRecord")
    @GetMapping("/checkAllTable/export")
    public void exportCheckTable(HttpServletResponse response, @RequestParam("starttimes") int starttimes, @RequestParam("endtimes") int endtimes, @RequestParam(value = "userids", required = false) String[] userids) throws Exception {
        //获取员工的打卡数据
        List<Map<String, Object>> l = qwCheckinsService.getCheckAllTables(starttimes, endtimes, userids);
        String title="和湛科技考勤表";
        XSSFWorkbook wb = ExcelUtil.exportExcel4(null, l, ExportQwCheckMonthTableDto.class, true,title,starttimes);
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String excelName = "员工考勤表";
        outputFile2007(response, wb, excelName);
    }

    private void outputFile2007(HttpServletResponse response, XSSFWorkbook wb, String excelName) throws Exception {
        //转码防止乱码
        response.addHeader("Content-Disposition", "attachment;filename=" + new String(excelName.getBytes("gb2312"), "ISO8859-1") + ".xlsx");
        OutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("coupon:qwcheckins:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = qwCheckinsService.queryPage(params);

        return R.ok(page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("coupon:qwcheckins:info")
    public R info(@PathVariable("id") Integer id) {
        QwCheckinsEntity qwCheckins = qwCheckinsService.getById(id);

        return R.ok(qwCheckins);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("coupon:qwcheckins:save")
    public R save(@RequestBody QwCheckinsEntity qwCheckins) {
        qwCheckinsService.save(qwCheckins);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("coupon:qwcheckins:update")
    public R update(@RequestBody QwCheckinsEntity qwCheckins) {
        qwCheckinsService.updateById(qwCheckins);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("coupon:qwcheckins:delete")
    public R delete(@RequestBody Integer[] ids) {
        qwCheckinsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
